<?php
class ScheduleModel extends CI_Model{

    public function __construct(){
        $this->load->database();
        parent:: __construct();
    }

    //only show  recently schedule
    public function getList($time){
        $data = array(
 //           'startTime >'=> $time
        );
       // $this->db->select('scheduleId as id, startTime as time, pathId as path');
      //  $query = $this->db->get_where("schedulelist", $data);

        $this->db->select('sd.scheduleId as id,sd.pathId as path, sf.shiftId, sf.scheduleDate as time');
        $this->db->join('schedulelist as sd', 'sf.shiftId=sd.scheduleId');
     //   $this->db->join('reservationlist as rl', 'rl.shiftId=sf.shiftId');
    //    $this->db->join('seatlist as st', 'rl.seatId=st.seatId');
        $query = $this->db->get_where("shiftlist as sf", $data);

       
        $result =  $query->result_array();


        //query remaing seats
        foreach($result as &$rs){

            //format time
            $rs['time'] = date("Y-m-d" ,strtotime($rs['time']));

            $this->db->select("st.seatNumber");
            $this->db->join('seatlist as st', "st.seatId=rl.seatId");
            $query = $this->db->get_where("reservationlist as rl", 
                array('userId'=>1111,
                    'rl.cancelTime'=>null,
                    'shiftId'=>$rs['shiftId'] ));

            $seatRs = $query->result_array(); 
            if(isset($seatRs[0])){
                $rs['seatsNumbers'] = $seatRs[0]['seatNumber'];
            }else{
                $rs['seatsNumbers'] = 0;
            }

            $this->db->select('reservationId');
            $query = $this->db->get_where("reservationlist", array(
                'cancelTime'=>null,
                'shiftId'=>$rs['shiftId'],
            ));
            $tmpRs= $query->result_array();
            $rs['remainingSeats'] = 60 -$query->num_rows();
        }

        return $result;
        
    }


    public function add($startPoint, $endPoint, $startTime, $desLocX, $desLocY){
        $data = array(
            'startPoint'=>$startPoint, 
            'endPoint'=>$endPoint, 
            'startTime'=>$startTime, 
            'destinationLocationX'=>$desLocX, 
            'destinationLocationY'=>$desLocY
        );
        $this->db->insert('schedulelist', $data); 
        return $this->db->affected_rows();

    }

    //获取行程状态
    public function getStatus($shiftIds){

        $shiftIdArr = explode(",",$shiftIds);

        $this->db->select('sfl.shiftId, sfl.scheduleDate,scl.startTime, sfl.arrivingTime');
        $this->db->join('shiftlist as sfl', 'scl.scheduleId=sfl.scheduleId');
        $this->db->where_in('sfl.shiftId', $shiftIdArr);
        $query = $this->db->get_where("schedulelist as scl", array());
        $tmpRs= $query->result_array();

        foreach($tmpRs as $key=>&$value){
            if( strtotime($value["scheduleDate"])< time()){
                $value["status"] =1;  //1表示已发车
                if( strtotime($value["arrivingTime"])< time()){
                    $value["status"] =2; //2表示已到达
                }
            }else{
                $value["status"] =0; //0表示未发车
            }
            unset($value['startTime']);
        }

        return $tmpRs;
    }


    public function getArrivingStatus($shiftIds){

        $shiftIdArr = explode(",",$shiftIds);

        $this->db->select('shiftId, arrivingTime');
        $this->db->where_in('shiftId', $shiftIdArr);

        $query = $this->db->get_where("shiftlist", array());
        $tmpRs= $query->result_array();

        foreach($tmpRs as $key=>&$value){
            if( strtotime($value["arrivingTime"])< time()){
                $value["status"] =1;
            }else{
                $value["status"] =0;
            }
            unset($value['arrivingTime']);
        }

        return $tmpRs;
    }

}
